package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.FavorableHouseDAO;
import com.foreveross.vo.FavorableHouse;

@Component("favorableHouseDAO")
public class FavorableHouseDAOImp extends SQLUtil implements FavorableHouseDAO {

	@Override
	public long findFavorableHouseListCount(String favorableId, String houseId)
			throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select count(distinct a.id) from favorable_house a ");
		sqlStr.append(" where 1=1 ");
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and a.house_id ='" + houseId + "'");
		}
		if(null!=favorableId && !"".equals(favorableId)){
			sqlStr.append(" and a.favorable_id = '" + favorableId + "'");
		}
		return Long.parseLong(findObejctBySQL(sqlStr.toString(),null).get(0).toString()) ;
	}
	
	@Override
	public List<FavorableHouse> findFavorableHouseList(String favorableId,
			String houseId,int pageIndex,int pageSize) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select distinct a.* from favorable_house a ");
		sqlStr.append(" where 1=1 ");
		
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and a.house_id ='" + houseId + "'");
		}
		if(null!=favorableId && !"".equals(favorableId)){
			sqlStr.append(" and a.favorable_id = '" + favorableId + "'");
		}
		return findObejctBySQL(sqlStr.toString(),FavorableHouse.class, pageIndex,
				pageSize);
	}

	@Override
	public void deleteFavorableHouse(String id) throws Exception {
		final String hql = "delete from FavorableHouse t where t.id='" + id + "'";
		updateObejctByHql(hql);
	}

	@Override
	public void deleteFavorableHouseByFavorableId(String favorableId)
			throws Exception {
		final String hql = "delete from FavorableHouse t where t.favorable.id='" + favorableId + "'";
		updateObejctByHql(hql);
	}

}
